package com.itheima.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Bean.Category;
import com.itheima.Bean.Employee;
import com.itheima.Service.CategoryService;
import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @GetMapping("/list")
    public R<List<Category>> getByType(Integer type){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type!=null,Category::getType,type);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }


    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增分类/套餐成功!");
    }

    @GetMapping("/page")
    public R<Page<Employee>> pageSelect(@RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "1") int pageSize){
        //创建page对象
        Page page1 = new Page(page,pageSize);

        LambdaQueryWrapper<Category> lqw  = new LambdaQueryWrapper();
        lqw.orderByAsc(Category::getSort);

        //调用Iservice的分页查询
        //把page对象和条件查询对象扔进去
        categoryService.page(page1,lqw);

        return R.success(page1);
    }

    @DeleteMapping
    public R<String> delete(Long ids){
//        return categoryService.removeById(ids) ?  R.success("删除成功！") : R.error("删除失败！");

        categoryService.remove(ids);
        return R.success("删除分类成功！");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }




}
